package com.howe.gateway.config;

import com.howe.gateway.balancer.CustomLoadBalancer;
import com.howe.gateway.filter.CustomLoadBalanceFilter;
import org.springframework.cloud.gateway.config.GatewayLoadBalancerProperties;
import org.springframework.cloud.gateway.filter.ReactiveLoadBalancerClientFilter;
import org.springframework.cloud.loadbalancer.core.ReactorServiceInstanceLoadBalancer;
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
import org.springframework.cloud.loadbalancer.support.LoadBalancerClientFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

@Configuration
public class CustomFilterConfiguration {

    @Bean
    public ReactorServiceInstanceLoadBalancer reactorServiceInstanceLoadBalancer(Environment environment,
                                                                                 LoadBalancerClientFactory loadBalancerClientFactory){
        String name = environment.getProperty(LoadBalancerClientFactory.PROPERTY_NAME);
        return new CustomLoadBalancer((loadBalancerClientFactory.getLazyProvider(name, ServiceInstanceListSupplier.class)),name);
    }

    @Bean
    public ReactiveLoadBalancerClientFilter reactiveLoadBalancerClientFilter(LoadBalancerClientFactory clientFactory,
                                                                             GatewayLoadBalancerProperties properties){
        return new CustomLoadBalanceFilter(clientFactory,properties);
    }

//    @Bean
//    @ConditionalOnEnabledGlobalFilter
//    public NettyRoutingFilter nettyRoutingFilter(HttpClient httpClient, ObjectProvider<List<HttpHeadersFilter>> headersFiltersProvider,
//                                                 HttpClientProperties properties){
//        return new CustomNettyRoutingFilter(httpClient,headersFiltersProvider,properties);
//    }
}
